var express = require('express');
var router = express.Router();
const db = require('../public/javascripts/db')
/* GET users listing. */
router.get('/', function (req, res, next) {
    // var owner_name = req.query.owner_name;
    var page = req.query.page || 1;
    var size = req.query.size || 5;
    var owner_name = req.query.owner_name;
    let sql = "";
    let sql2 = "";
    let data = null;
    let start = (page - 1) * size;

    if (owner_name) {
        sql = "select * from owner where owner_name like ? order by owner_id desc limit " + start + ", " + size;
        sql2 = "select count(*) from owner where owner_name like ?";
        data = ["%" + owner_name + "%"];
    } else {
        sql = `select * from owner order by owner_id desc limit ${start}, ${size}`;
        sql2 = "select count(*) from owner";
    }
    db(sql2, data, (result) => {
        db(sql, data, (result2) => {
            console.log(result2);
            if (result2.length > 0) {
                res.json({
                    code: 200,
                    message: "查询成功",
                    data: result2,
                    total: result[0]['count(*)']
                })
            } else {
                res.json({
                    code: 201,
                    message: "查询失败"
                })
            }
        })
    })

});

module.exports = router;